var quickViewController = {
    config:{
        aryId: '',
        urlAjax: ''
    },
    
    init: function(urlAjax){
        quickViewController.config.urlAjax = urlAjax;
        //        alert(quickViewController.config.urlAjax);
        $('#qv_overlay').live('click',function(){
            $('#qv_overlay').fadeOut(400);
            $('#qv_maincontent').fadeOut(400);
        });
    },
    
    detectWindow: function(){
        var widthWin = $(window).width();
        var heightWin = $(window).height();
        var offsetWin = $(window).scrollTop();
        //var offset = $(item).offset();							
        var widthQV = ($('#qv_maincontent').width())?$('#qv_maincontent').width():648;
        var top = (heightWin - 500)/2 + $(window).scrollTop();
        var left = (widthWin - 600)/2;				
        //var distanceTop = offset.top -  $(window).scrollTop();	
        //top = (distanceTop > (heightWin/2 - 30)&&(distanceTop > 380))?(-380):top;
        //left = (offset.left < left)?(offset.left - 20):left;
        //left = ((widthWin-offset.left)<(widthQV-left+52))?(widthQV - widthWin + offset.left + 52): left;
        $('#qv_maincontent').css({
            'top' : top + 'px', 
            'left' : left +  'px'
        });
    },
    
    viewData:function(product_id){
        quickViewController.showOverlay();
        if($("#qv_content").data(product_id)){
            var data = $('#qv_content').data(product_id);
            quickViewController.updateData(data);
        }else{
            quickViewController.ajaxLoadData(product_id);
        }
    },
    
    nextView:function(){
        var aryId = $("#data_qv").data("ary_id");
        var product_id = $("#data_qv").data("next_qv");
        if($('#qv_content').data(product_id)){
            var data = $('#qv_content').data(product_id);
            quickViewController.updateData(data);
        }else{
            quickViewController.ajaxLoadData(product_id);
        }
        var totalProduct = aryId.length;
        for(i in aryId){
            if(product_id == aryId[i]){
                returnValue = i;
            }
        }
        if(returnValue == 0){
            $('#qv_pre').addClass('hidden');
            $('#qv_next').removeClass('hidden');
        } else if(returnValue == (parseInt(totalProduct)-1)){
            $('#qv_next').addClass('hidden');
            $('#qv_pre').removeClass('hidden');
        } else{
            $('#qv_next').removeClass('hidden');
            $('#qv_pre').removeClass('hidden');
        }
        var returnValueNext = parseInt(returnValue)+1;
        var returnValuePrev = parseInt(returnValue)-1;
        $('#data_qv').data('next_qv',aryId[returnValueNext]);
        $('#data_qv').data('prev_qv',aryId[returnValuePrev]);
    },
    
    prevView:function(){
        var aryId = $("#data_qv").data("ary_id");
        var product_id = $("#data_qv").data("prev_qv");
        if($('#qv_content').data(product_id)){
            var data = $('#qv_content').data(product_id);
            quickViewController.updateData(data);
        }else{
            quickViewController.ajaxLoadData(product_id);
        }
        var totalProduct = aryId.length;
        for(i in aryId){
            if(product_id == aryId[i]){
                returnValue = i;
            }
        }
        if(returnValue == 0){
            $('#qv_pre').addClass('hidden');
            $('#qv_next').removeClass('hidden');
        } else if(returnValue == (parseInt(totalProduct)-1)){
            $('#qv_next').addClass('hidden');
            $('#qv_pre').removeClass('hidden');
        } else{
            $('#qv_next').removeClass('hidden');
            $('#qv_pre').removeClass('hidden');
        }
                
        var returnValueNext = parseInt(returnValue)+1;
        var returnValuePrev = parseInt(returnValue)-1;
        $('#data_qv').data('next_qv',aryId[returnValueNext]);
        $('#data_qv').data('prev_qv',aryId[returnValuePrev]);
    },
    
    showOverlay: function(){
        $('#qv_overlay').fadeIn(400);
        $('#qv_maincontent').fadeIn(400);
        quickViewController.detectWindow();
    },
    
    updateData:function(data){
        $('#qv_content').html(data['content']);
    },
    
    ajaxLoadData:function(product_id){
        $.getJSON(quickViewController.config.urlAjax,{
            'product_id':product_id
        }, function(data) {
            if(data['success']){
                $('#qv_content').data(product_id, data)
                $('#qv_content').html(data['content']);
            }
        });
    },
    
    getListId:function(event, product_id, view){
        var aryId = new Array();
        switch (view) {
            case "list_carousel":
                $(event).parents('ul').children().each(function(){
                    aryId.push($(this).attr('name'));
                });
        }
        var totalProduct = aryId.length;
        var returnValue = 0;
        for(i in aryId){
            if(product_id == aryId[i]){
                returnValue = i;
            }
        }
        if(returnValue == 0){
            $('#qv_pre').addClass('hidden');
            $('#qv_next').removeClass('hidden');
        } else if(returnValue == (parseInt(totalProduct)-1)){
            $('#qv_next').addClass('hidden');
            $('#qv_pre').removeClass('hidden');
        } else{
            $('#qv_next').removeClass('hidden');
            $('#qv_pre').removeClass('hidden');
        }
        var returnValueNext = parseInt(returnValue)+1;
        var returnValuePrev = parseInt(returnValue)-1;
        $('#data_qv').data('next_qv',aryId[returnValueNext]);
        $('#data_qv').data('prev_qv',aryId[returnValuePrev]);
        $('#data_qv').data('ary_id',aryId);
    }
    
};